<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
</head>
<html>
<body>
    <h2>二分查找（严格递增）</h2>
    <p>查询数列：</p><p id="arr"></p>
    <form>
        查询数：<input id="num" type="text" placeholder="请输入所要查询的数"/>
    </form>
    <p>查询数位置：</p><p id="demo"></p>
    <script>
    var A=[1,4,5,7,8,9,10]
    document.getElementById("arr").innerHTML = A.toString();
    var x=document.getElementById("num")
    function binarySearch(A,left,right,x)
    {
	    var mid,ans;
	    while(left<=right)
	{
		mid=(left+right)/2;
		if(A[mid]==x) {ans=mid;break; }
		else if(A[mid]>x) right=mid-1;  
		else left=mid+1; 
	}
    if(left>right) ans=-1;//查找失败
    document.getElementById("demo").innerHTML = ans.toString();
    } 
    </script>
<button type="button" onclick="binarySearch(A,0,6,x.value)">查找</button>
</body>
</html>